import sys
sys.path.append("../")

from model.StockBaseInfo import *
import time
from datetime import datetime
import tkinter as tk

base = StockBaseInfo()

# 股票数据获取
def get_news():
    p_list = []
    # 300671 富满电子
    # 600460 士兰微
    # 300777 国民技术
    ### 每次麻烦的就是需要在下面更新股票代码
    news = base.getStockPriceByDFCF(base.translateDfcfCode('300077'))
    # 昨日收盘价
    start_pri = news[0]["f18"]
    # 今日开盘价
    yes_pri = news[0]["f17"]
    # 现价 实时
    now_pri = news[0]["f2"]
    # 涨跌幅度
    up_num = round(((float(now_pri) - float(yes_pri)) / float(yes_pri)), 4) * 100
    # up_num = round(up_num, 4)*100
    if up_num > 0:
        up_num = "+" + str(up_num)
    else:
        up_num = str(up_num)
    p_list.append(datetime.now().strftime('%H:%M:%S'))
    #p_list.append(news[0]["f12"])
    p_list.append("")
    p_list.append(up_num)

    return p_list


# 数据刷新的回调
def refreshDate():
    # 获取数据
    p_list = get_news()
    # 分别使用上面获取的数据, 填充text
    text1.delete(0.0, tk.END)
    text1.insert(tk.INSERT, p_list[0])
    text1.update()

    text2.delete(0.0, tk.END)
    text2.insert(tk.INSERT, p_list[1])
    text2.update()

    text3.delete(0.0, tk.END)
    text3.insert(tk.INSERT, p_list[2])
    text3.update()

    windows.after(60000, refreshDate)


# 显示价格
def show_price():
    # 定义全局变量, 目的是在refreshText 中可以调用这几个变量
    global text1
    global text2
    global text3
    global windows

    # 创建基础窗口
    windows = tk.Tk()
    windows.geometry('130x140+1775+680')  ## 规定窗口大小500*500像素
    windows.resizable(False, False)  ## 规定窗口不可缩放

    # 定义组件
    label1 = tk.Label(windows, text="N_T:")
    label1.grid(row=0, column=1, padx=2, pady=2)

    text1 = tk.Text(windows, width=8, height=1)
    text1.grid(row=0, column=2, padx=10, pady=10)

    label2 = tk.Label(windows, text="N_P:")
    label2.grid(row=1, column=1, padx=2, pady=2)

    text2 = tk.Text(windows, width=8, height=1)
    text2.grid(row=1, column=2, padx=10, pady=10)

    label3 = tk.Label(windows, text="U_P:")
    label3.grid(row=2, column=1, padx=2, pady=2)

    text3 = tk.Text(windows, width=8, height=1)
    text3.grid(row=2, column=2, padx=10, pady=10)

    windows.after(60000, refreshDate)
    windows.wm_attributes('-topmost', 1)
    windows.mainloop()


if __name__ == '__main__':
    show_price()
